
/*---------------------------------------------------------

  This file should contain variables and functions that are 
   the same on both client and server.

  This file will get sent to the client - so don't add 
   anything to this file that you don't want them to be
   able to see.

---------------------------------------------------------*/

include( 'obj_player_extend.lua' )
include( 'plyer_extension.lua' )
include( 'gravitygun.lua' )
include( 'player_shd.lua' )

GM.Name 		= "desperate defense"
GM.Author 		= "kraizer793"
GM.Email 		= "baupshaw@gmail.com"
GM.Website 		= "www.dont-go-here.stoopid"
GM.TeamBased 		= false

DDSpawns = 50
function GM:PlayerHurt( player, attacker, healthleft, healthtaken )
	//define some local variables for smplicity
	local model = player:GetModel()
	local health = healthleft
	local dmg = healthtaken
	local sound = "vo/npc/barney/ba_pain0" .. math.random(1,9) .. ".wav"

//Alyx
if model == "models/player/alyx.mdl" then
local decider = math.random(1,4)
if decider <= 3 then sound = "vo/npc/alyx/hurt0" .. math.random(4,6) .. ".wav" end
if decider == 4 then sound = "vo/npc/alyx/hurt08.wav" end
end

//Barney
if model == "models/player/barney.mdl" then
local decider = math.random(1,12)
if decider <=9 then sound = "vo/npc/barney/ba_pain0" .. math.random(1,9) .. ".wav" end
if decider == 10 then sound = "vo/npc/barney/ba_pain10.wav" end
if decider == 11 then sound = "vo/k_lab/ba_geethanks.wav" end
if decider == 12 then sound = "vo/npc/barney/ba_ohshit03.wav" end
end

//Breen
if model == "models/player/breen.mdl" then
local decider = math.random(1,5)
if decider == 1 then sound = "vo/citadel/br_gravgun.wav" end
if decider == 2 then sound = "vo/citadel/br_no.wav" end
if decider == 3 then sound = "vo/citadel/br_mock04.wav" end
if decider == 4 then sound = "vo/citadel/br_ohshit.wav" end
if decider == 5 then sound = "vo/citadel/br_yesjudith.wav" end
end


//Female Rebel
if model == "models/player/Group01/female_01.mdl"
|| model == "models/player/Group01/female_02.mdl"
|| model == "models/player/Group01/female_03.mdl"
|| model == "models/player/Group01/female_04.mdl"
|| model == "models/player/Group01/female_06.mdl"
|| model == "models/player/Group01/female_07.mdl"
|| model == "models/player/Group03/female_01.mdl"
|| model == "models/player/Group03/female_02.mdl"
|| model == "models/player/Group03/female_03.mdl"
|| model == "models/player/Group03/female_04.mdl"
|| model == "models/player/Group03/female_06.mdl"
|| model == "models/player/Group03/female_07.mdl"
then
sound = "vo/npc/female01/pain0" .. math.random(1,9) .. ".wav"
end

//Male Rebel
if model == "models/player/Group01/male_01.mdl"
|| model == "models/player/Group01/male_02.mdl"
|| model == "models/player/Group01/male_03.mdl"
|| model == "models/player/Group01/male_04.mdl"
|| model == "models/player/Group01/male_05.mdl"
|| model == "models/player/Group01/male_06.mdl"
|| model == "models/player/Group01/male_07.mdl"
|| model == "models/player/Group01/male_08.mdl"
|| model == "models/player/Group01/Male_09.mdl"
|| model == "models/player/Group03/male_01.mdl"
|| model == "models/player/Group03/male_02.mdl"
|| model == "models/player/Group03/male_03.mdl"
|| model == "models/player/Group03/male_04.mdl"
|| model == "models/player/Group03/male_05.mdl"
|| model == "models/player/Group03/male_06.mdl"
|| model == "models/player/Group03/male_07.mdl"
|| model == "models/player/Group03/male_08.mdl"
|| model == "models/player/Group03/male_09.mdl"
then
sound = "vo/npc/male01/pain0"..math.random(1,9)..".wav"
end

//G-Man
if model == "models/player/gman_high.mdl" then
sound = "npc/barnacle/neck_snap" .. math.random(1,2) .. ".wav"
end

//Kleiner
if model == "models/player/Kleiner.mdl" then
local decider = math.random(1,4)
if decider == 1 then sound = "vo/k_lab/kl_hedyno03.wav" end
if decider == 2 then sound = "vo/k_lab/kl_ahhhh.wav" end
if decider == 3 then sound = "vo/k_lab/kl_fiddlesticks.wav" end
if decider == 4 then sound = "vo/k_lab/kl_ahhhh.wav" end
end

//Father Grigori is being left out...
//All his VO's are senseless rantings...

//Mossman
if model == "models/player/mossman.mdl" then
local decider = math.random(1,2)
if decider == 1 then sound = "vo/eli_lab/mo_airlock0" .. math.random(1,2) .. ".wav" end
if decider == 2 then sound = "vo/eli_lab/mo_airlock0" .. math.random(3,4) .. ".wav" end
end

//Odessa is also left out...
//Not too much to say from him

//Eli Vance
if model == "models/player/eli.mdl" then
local decider = math.random(1,3)
if decider == 1 then sound = "vo/eli_lab/eli_wantyou.wav" end
if decider == 2 then sound = "vo/eli_lab/eli_goodvort.wav" end
if decider == 3 then sound = "vo/eli_lab/eli_lookaround.wav" end
end

//Metrocop
if model == "models/player/police.mdl" then
sound = "npc/metropolice/die" .. math.random(1,4) .. ".wav"
end

//Magnusson - Where are his VO's?
//if model == "models/player/magnusson.mdl" then
//sound = "npc/metropolice/die1.wav"
//end

util.PrecacheSound(sound)
player:EmitSound(sound, 72, 100)
end

/*---------------------------------------------------------
   Name: gamemode:KeyPress( )
   Desc: Player pressed a key (see IN enums)
---------------------------------------------------------*/
function GM:KeyPress( player, key )
end


/*---------------------------------------------------------
   Name: gamemode:KeyRelease( )
   Desc: Player released a key (see IN enums)
---------------------------------------------------------*/
function GM:KeyRelease( player, key )
end


/*---------------------------------------------------------
   Name: gamemode:PlayerConnect( )
   Desc: Player has connects to the server (hasn't spawned)
---------------------------------------------------------*/
function GM:PlayerConnect( name, address )
end

/*---------------------------------------------------------
   Name: gamemode:PlayerAuthed( )
   Desc: Player's STEAMID has been authed
---------------------------------------------------------*/
function GM:PlayerAuthed( ply, SteamID, UniqueID )
end

/*---------------------------------------------------------
   Set the owner of props that are spawned.
   This is used by a few scripts to "refund"
   props that are removed by various entities.
---------------------------------------------------------*/
function SpawnedProp(ply, model, ent)
	if ply:Valid() then
		ent:SetOwner(ply)
	end
end

hook.Add("PlayerSpawnedProp", "playerSpawnedProp", SpawnedProp) 
/*---------------------------------------------------------
   Name: gamemode:PropBreak( )
   Desc: Prop has been broken
---------------------------------------------------------*/
function GM:PropBreak( attacker, prop )
end


/*---------------------------------------------------------
   Name: gamemode:PhysgunPickup( )
   Desc: Return true if player can pickup entity
---------------------------------------------------------*/
function GM:PhysgunPickup( ply, ent )

	// Don't pick up players
	if ( ent:GetClass() == "player" ) then return false end

	return true
end


/*---------------------------------------------------------
   Name: gamemode:PhysgunDrop( )
   Desc: Dropped an entity
---------------------------------------------------------*/
function GM:PhysgunDrop( ply, ent )
end


/*---------------------------------------------------------
   Name: gamemode:SetupMove( player, movedata )
   Desc: Allows us to change stuff before the engine 
		  processes the movements
---------------------------------------------------------*/
function GM:SetupMove( ply, move )
end


/*---------------------------------------------------------
   Name: gamemode:FinishMove( player, movedata )
---------------------------------------------------------*/
function GM:FinishMove( ply, move )

end

/*---------------------------------------------------------
   Name: gamemode:Move
   This basically overrides the NOCLIP, PLAYERMOVE movement stuff.
   It's what actually performs the move. 
   Return true to not perform any default movement actions. (completely override)
---------------------------------------------------------*/
function GM:Move( ply, mv )
end

/*---------------------------------------------------------
   Name: gamemode:PlayerShouldTakeDamage
   Return true if this player should take damage from this attacker
---------------------------------------------------------*/
function GM:PlayerShouldTakeDamage( ply, attacker )
	return true
end

/*---------------------------------------------------------
   Name: gamemode:ContextScreenClick(  aimvec, mousecode, pressed, ply )
   'pressed' is true when the button has been pressed, false when it's released
---------------------------------------------------------*/
function GM:ContextScreenClick( aimvec, mousecode, pressed, ply )
	
	// We don't want to do anything by default, just feed it to the weapon
	local wep = ply:GetActiveWeapon()
	if ( ValidEntity( wep ) && wep.ContextScreenClick ) then
		wep:ContextScreenClick( aimvec, mousecode, pressed, ply )
	end
	
end

/*---------------------------------------------------------
   Name: Text to show in the server browser
---------------------------------------------------------*/
function GM:GetGameDescription()
	return self.Name
end


/*---------------------------------------------------------
   Name: Saved
---------------------------------------------------------*/
function GM:Saved()
end


/*---------------------------------------------------------
   Name: Restored
---------------------------------------------------------*/
function GM:Restored()
end


/*---------------------------------------------------------
   Name: EntityRemoved
   Desc: Called right before an entity is removed. Note that this
   isn't going to be totally reliable on the client since the client
   only knows about entities that it has had in its PVS.
---------------------------------------------------------*/
function GM:EntityRemoved( ent )
end


/*---------------------------------------------------------
   Name: Tick
   Desc: Like Think except called every tick on both client and server
---------------------------------------------------------*/
function GM:Tick()
end

/*---------------------------------------------------------
   Name: OnEntityCreated
   Desc: Called right after the Entity has been made visible to Lua
---------------------------------------------------------*/
function GM:OnEntityCreated( Ent )
end

/*---------------------------------------------------------
   Name: gamemode:EntityKeyValue( ent, key, value )
   Desc: Called when an entity has a keyvalue set
	      Returning a string it will override the value
---------------------------------------------------------*/
function GM:EntityKeyValue( ent, key, value )
end

/*---------------------------------------------------------
   Name: gamemode:CreateTeams()
   Desc: Note - HAS to be shared.
---------------------------------------------------------*/
function lolteams()
	print("Teams NOT used in DDefense")
end

//-----CLASSES

class = {}

--add classes like below, copy & paste and rename the class
class["Soldier"] = {}
class["Soldier"].Weapons ={"weapon_g36e", "weapon_p99", "weapon_frag" }
class["Soldier"].Ammo = {}
class["Soldier"].Ammo.weapon_g36e_primary = 250
class["Soldier"].Ammo.weapon_p99_primary = 90
class["Soldier"].Speed = {}
class["Soldier"].Speed.Walk = 225
class["Soldier"].Speed.Run = 400
class["Soldier"].Health = 125
class["Soldier"].Model = "models/player/Group03/male_09.mdl"
class["Soldier"].Desc = "The unsung hero of the war."

class["Heavy Gunner"] = {}
class["Heavy Gunner"].Weapons ={"weapon_saw", "weapon_p99", "weapon_frag" }
class["Heavy Gunner"].Ammo = {}
class["Heavy Gunner"].Ammo.weapon_saw_primary = 350
class["Heavy Gunner"].Ammo.weapon_p99_primary = 90
class["Heavy Gunner"].Speed = {}
class["Heavy Gunner"].Speed.Walk = 225
class["Heavy Gunner"].Speed.Run = 350
class["Heavy Gunner"].Health = 175
class["Heavy Gunner"].Model = "models/player/barney.mdl"
class["Heavy Gunner"].Desc = "Has the ability to knock out enemies quickly and effeciently."

class["Sniper"] = {}
class["Sniper"].Weapons ={"weapon_rifle", "weapon_p99", "weapon_frag" }
class["Sniper"].Ammo = {}
class["Sniper"].Ammo.weapon_rifle_primary = 20
class["Sniper"].Ammo.weapon_p99_primary = 90
class["Sniper"].Speed = {}
class["Sniper"].Speed.Walk = 200
class["Sniper"].Speed.Run = 350
class["Sniper"].Health = 100
class["Sniper"].Model = "models/player/alyx.mdl"
class["Sniper"].Desc = "Picks 'em off like fleas"

class["Demolisher"] = {}
class["Demolisher"].Weapons ={"weapon_rpg", "weapon_p99", "weapon_axe" }
class["Demolisher"].Ammo = {}
class["Demolisher"].Ammo.weapon_rpg_primary = 3
class["Demolisher"].Ammo.weapon_p99_primary = 90
class["Demolisher"].Speed = {}
class["Demolisher"].Speed.Walk = 200
class["Demolisher"].Speed.Run = 350
class["Demolisher"].Health = 120
class["Demolisher"].Model = "models/player/eli.mdl"
class["Demolisher"].Desc = "KA-BOOOM!"

class["Defender"] = {}
class["Defender"].Weapons ={"weapon_quadshot", "weapon_p99", "weapon_frag" }
class["Defender"].Ammo = {}
class["Defender"].Ammo.weapon_quadshot_primary = 24
class["Defender"].Ammo.weapon_p99_primary = 90
class["Defender"].Speed = {}
class["Defender"].Speed.Walk = 225
class["Defender"].Speed.Run = 400
class["Defender"].Health = 175
class["Defender"].Model = "models/player/monk.mdl"
class["Defender"].Desc = "A vanguard for those who follow behind."

class["Ranger"] = {}
class["Ranger"].Weapons ={"weapon_SCAR", "weapon_p99", "weapon_frag" }
class["Ranger"].Ammo = {}
class["Ranger"].Ammo.weapon_SCAR_primary = 315
class["Ranger"].Ammo.weapon_p99_primary = 90
class["Ranger"].Speed = {}
class["Ranger"].Speed.Walk = 225
class["Ranger"].Speed.Run = 500
class["Ranger"].Health = 100
class["Ranger"].Model = "models/player/Group03/male_01.mdl"
class["Ranger"].Desc = "The ones get it done, and quick"

class["Interceptor"] = {}
class["Interceptor"].Weapons ={"weapon_uzi", "weapon_p99", "weapon_frag" }
class["Interceptor"].Ammo = {}
class["Interceptor"].Ammo.weapon_uzi_primary = 360
class["Interceptor"].Ammo.weapon_p99_primary = 90
class["Interceptor"].Speed = {}
class["Interceptor"].Speed.Walk = 350
class["Interceptor"].Speed.Run = 650
class["Interceptor"].Health = 75
class["Interceptor"].Model = "models/player/breen.mdl"
class["Interceptor"].Desc = "An expert at getting in and getting out as quickly as possible."

class["Engineer"] = {}
class["Engineer"].Weapons ={"weapon_sten", "weapon_p99", "weapon_frag" }
class["Engineer"].Ammo = {}
class["Engineer"].Ammo.weapon_sten_primary = 100
class["Engineer"].Ammo.weapon_p99_primary = 90
class["Engineer"].Speed = {}
class["Engineer"].Speed.Walk = 300
class["Engineer"].Speed.Run = 525
class["Engineer"].Health = 80
class["Engineer"].Model = "models/player/kliener.mdl"
class["Engineer"].Desc = "Builds things."


if (SERVER) then
AddCSLuaFile("client/classmenu_cl.lua")
AddCSLuaFile("Classes.lua")
function ChangeClass(ply,com,arg)
	local name = tostring(arg[1])
	if !class[name] then return end
	if ply.InitialSpawn then
		ply.Class = name
		ply:StripWeapons()
		ChangeClass2(ply)
		ply.InitialSpawn = false
	else
		//change class and die
		ply.Class = name
		ply:KillSilent()
	end
end
concommand.Add("_Changeclass", ChangeClass)

 function FirstSpawn( ply )  	  	
ply:SendLua("ClassMenuShow()")
ply.InitialSpawn = true
ply.Class = nil
 end     
 hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn );  

 function Loadout( ply )  	
	ChangeClass2(ply)
	return true // this prevents the gamemode's default PlayerLoadout from getting called (this only applies when you're not coding the gamemode itself)  
 end     
 hook.Add( "PlayerLoadout", "gravAndShot", Loadout)  
 
 function ChangeClass2(ply)
 	local name = ply.Class
	if ply.Class and class[name] then
		for i,v in pairs(class[name].Weapons) do
			//weapon
			ply:Give(v)
			//ammo 
			if class[name].Ammo then
			if class[name].Ammo[v.."_primary"] then
				ply:SetAmmo(class[name].Ammo[v.."_primary"],ply:GetWeapon(v):GetPrimaryAmmoType( ))
			end
			if class[name].Ammo[v.."_secondary"] then
				ply:SetAmmo(class[name].Ammo[v.."_secondary"],ply:GetWeapon(v):GetSecondaryAmmoType( ))
			end
			end
		end
		//playermodel
		if class[name].Model then
			timer.Simple(0.1,function() ply:SetModel(class[name].Model) end)
		end
		//health
		if class[name].Health then
			ply:SetMaxHealth(class[name].Health) 
			ply:SetHealth(class[name].Health)
		end
		//speed
		if class[name].Speed then
			timer.Simple(0.1,function() GAMEMODE:SetPlayerSpeed(ply, class[name].Speed.Walk, class[name].Speed.Run) end)
		end
		
	end	
 end
 
end
if (CLIENT) then

function GetString(name)
	local hstr;local sstr
	if class[name].Health then
		hstr = "Health: "..class[name].Health.."\n"
	else
		hstr = ""
	end
	if class[name].Speed then
		sstr = "Walk Speed: "..class[name].Speed.Walk.."\n"
		sstr = sstr.."Run Speed: "..class[name].Speed.Run.."\n"
	else
		sstr = ""
	end
	local wstr = "Weapons:\n"
	//if !class[name] then return str end
	for p,o in pairs(class[name].Weapons) do
		wstr = wstr..o.."\n"
	end
	local str = hstr..sstr..wstr
	return str
end
function ClassMenuShow()
	local classname = "nothing"
	local w,h = ScrW()/2, ScrH()/2
	local Frame = vgui.Create( "DFrame" ); //Create a frame 
	local icon = vgui.Create( "DModelPanel", Frame ) 
	Frame:SetSize(w+15,h ); 
	Frame:SetPos( 100, 100 ); //Move the frame to 100,100 
	Frame:SetVisible( true ); //Make the frame 
	Frame:MakePopup( ); //Visible 
	Frame:SetTitle("Choose your class" ); //Set the title to "This is the title" 
	function Frame:Paint()
		draw.RoundedBox( 8, 0,0,w+15,h, Color(56,56,56,240) )  
		surface.SetDrawColor( 75,75,75,200 )      
		surface.DrawLine( 0,20,w,20) 
		draw.RoundedBox( 8, 30+(w/4)+w/3, 26, w/3, h/2 - 35, Color(50,50,75,200) )  
		draw.RoundedBox( 8, 30+(w/4)+w/3, h/2 - 4, w/3, h/2 - 40, Color(50,50,75,200) ) 
	end
	local List = vgui.Create("DPanelList", Frame) 
	List:EnableHorizontal(false) // disable horizontal scroll 
	List:EnableVerticalScrollbar(true) // enable vertical scroll  
	local myLabel= vgui.Create("DLabel", Frame)  
	myLabel:SetPos(38+(w/4)+w/3, 30)
	myLabel:SetText("Weapons:\n")
	myLabel:SizeToContents() 
	local myLabel2= vgui.Create("DLabel", Frame)  
	myLabel2:SetPos(38+(w/4)+w/3,  h/2)
	myLabel2:SetText("Description:\n")
	myLabel2:SizeToContents() 
	
	function icon.LayoutEntity( icon, me )  
	icon.RunAnimation( icon ) 
	end 
	icon:SetPos(25+(w/4)-(w/4-w/6), w/8 + 20)
	icon:SetSize(w/2,w/2)
	icon:SetCamPos( Vector( 40, 0, 50 ) ) 
	icon:SetLookAt( Vector( 0, 0, 50 ) ) 
	icon:SetFOV( 90 ) 
		
	for i,v in pairs(class) do
		local Button = vgui.Create( "DButton", Frame ); //Create a button that is attached to Frame 
		Button:SetText( i ); //Set the button's text to the name of the class 
		List:AddItem(Button)
		function Button:DoClick( ) //This is called when the button is clicked 
			myLabel:SetText(GetString(i))  
			local desc = class[i].Desc or ""
			myLabel2:SetText("Description:\n"..desc)
			timer.Simple(0.1, function() myLabel:SizeToContents() end)
			timer.Simple(0.1, function() myLabel2:SizeToContents() end)
			classname = i
			if class[i].Model then
				icon:SetModel( class[i].Model ) 
			else
				icon:SetModel( LocalPlayer():GetModel() ) 
			end
		end	


	end
	List:SetSize(w/4,h-40-40)
	List:SetPos(30,30)	
	local Button2 = vgui.Create( "DButton", Frame ); //Create a button that is attached to Frame 
	Button2:SetText( "Choose Class" ); //Set the button's text to "Click me!" 
	Button2:SetSize(w/4-40,20)
	Button2:SetPos(50,h-40)
	function Button2:DoClick()
		RunConsoleCommand("_ChangeClass", classname)
		Frame:SetVisible(false)
	end
  
	function ChatTextClassMenu( playerindex, playername, text )  
		if LocalPlayer() == player.GetByID(playerindex) then  	
		if text == "/changeclass" then
			if Frame:IsValid() then
				Frame:SetVisible(true)
			else
				ClassMenuShow()
			end
		end
		end
	end  // Hook the event.  
	hook.Add( "ChatText", "ChatTextClassMenu", ChatTextClassMenu );
end
end

//--------This is the money system, get points for assplodin' NPC's
local function KrOnNPCKilled( victim, killer, weapon )
	if not (killer and killer:IsValid() and killer:IsPlayer()) then return end
	local NPCKillCount = killer:GetNetworkedInt("npckillcount") or 0
	if not (NPCKillCount--[[ and NPCKillCount:IsValid()]]) then
		NPCKillCount = 0
	end
	local VictimType = victim:GetClass()
	local ScoreAdd = 0
	if (VictimType == "npc_combine_s") then
		ScoreAdd = 50
	elseif (VictimType == "npc_antlion") then
		ScoreAdd = 50
	elseif (VictimType == "npc_antlion_worker") then
		ScoreAdd = 100
	elseif (VictimType == "npc_antlionguard") then
		ScoreAdd = 250
	elseif (VictimType == "npc_hunter") then
		ScoreAdd = 250
	elseif (VictimType == "npc_metropolice") then
		ScoreAdd = 25
	elseif (VictimType == "npc_combinegunship") then
		ScoreAdd = 500
	end
	NPCKillCount = NPCKillCount + ScoreAdd
	killer:SetNetworkedInt("npckillcount",NPCKillCount)
end

hook.Add("OnNPCKilled","KrOnNPCKilled",KrOnNPCKilled)

function GM:CalcView(ply,pos,ang,fov)
	local rag = ply:GetRagdollEntity()
	if ValidEntity(rag) then
		local att = rag:GetAttachment(rag:LookupAttachment("eyes"))
		return self.BaseClass:CalcView(ply,att.Pos,att.Ang,fov)
	end
	return self.BaseClass:CalcView(ply,pos,ang,fov)
end
